package com.ideas.impl.mianshi;

import java.util.Random;

/**
 * 模拟3个线程进行猜数字游戏，一个线程给出目标数字0-100，2个线程去猜并得到猜测结果是大了还是小了还是猜对了，直到猜中为止
 *
 * ps：wait()、notifyAll()必须得在同步块中使用，否则会报错。凡事往简单处想，往认真处行。不过下面答案没有体现出多个线程间的通信！！
 * @Description
 * @Author lijian.wu
 * @Date 2025-1-21 10:11
 */
public class GuessNumber {

    //在生成目标数字后就停止运行了，和原题意思不符
    static class NumberGenerator implements Runnable{

        private int number;
        private boolean numberGenerated = false;

        public synchronized int getNumber() {
            while (!numberGenerated) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return number;
        }

        @Override
        public void run() {
            generateNumber();
        }

        private synchronized void generateNumber() {
            number = new Random().nextInt(101);
            System.out.println("生成目标数: " + number);
            numberGenerated = true;
            notifyAll();
        }
    }

    static class NumberGuesser implements Runnable{
        //猜的数字
        private int guess;
        //目标数字
        private int number;
        //是否猜中了
        private boolean guessed;
        private NumberGenerator numberGenerator;
        //猜的数字的最小值
        private int left = 0;
        //猜的数字的最大值
        private int right = 100;
        public NumberGuesser(NumberGenerator numberGenerator) {
            this.numberGenerator = numberGenerator;
        }
        public void guessNumber() {
            while (!guessed) {
                guess = new Random().nextInt(right - left + 1) + left;
                System.out.println(Thread.currentThread().getName() + " guess : " + guess);
                if(guess < number){
                    System.out.println("too low");
                    left = guess;
                }else if(guess > number){
                    System.out.println("too high");
                    right = guess;
                }else{
                    System.out.println("correct");
                    guessed = true;
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        }
        @Override
        public void run() {
            number = numberGenerator.getNumber();
            guessNumber();
        }
    }
    public static void main(String[] args) {
        NumberGenerator numberGenerator = new NumberGenerator();
        Thread numberGeneratorThread = new Thread(numberGenerator);

        NumberGuesser numberGuesser1 = new NumberGuesser(numberGenerator);
        NumberGuesser numberGuesser2 = new NumberGuesser(numberGenerator);
        Thread numberGuesserThread1 = new Thread(numberGuesser1);
        Thread numberGuesserThread2 = new Thread(numberGuesser2);

        numberGeneratorThread.start();
        numberGuesserThread1.start();
        numberGuesserThread2.start();
    }
}
